using Newtonsoft.Json;
using LiJ.Common.Core;
using LiJ.Http.Mos;
using LiJ.Framework.Official.Basic.Mos;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace LiJ.Framework.Official.Basic
{
	/// <summary>
	///  公号用户管理接口类
	/// </summary>
	public class WxOffUserApi : WxOffBaseApi
	{
		/// <summary>
		///
		/// </summary>
		/// <param name="config">配置信息，如果这里不传，需要在程序入口静态 WxBaseApi.DefaultConfig 属性赋值</param>
		public WxOffUserApi(AppConfig config = null)
			: base(config)
		{
		}

		/// <summary>
		/// 获取用户openid列表
		/// </summary>
		/// <param name="next_openid">第一个拉取的OPENID，不填默认从头开始拉取</param>
		/// <returns></returns>
		public async Task<WxOpenIdsResp> GetOpenIdListAsync(string next_openid = "")
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Get;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/user/get?next_openid=" + next_openid;
			return await base.RestCommonOffcialAsync<WxOpenIdsResp>(LHttpRequest, null);
		}

		/// <summary>
		///   设置用户备注名
		/// </summary>
		/// <param name="openid"></param>
		/// <param name="remark">备注名称</param>
		/// <returns></returns>
		public async Task<WxBaseResp> SetUserRemarkAsync(string openid, string remark)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/user/info/updateremark";
			LHttpRequest.CustomBody = string.Format("{{\"openid\":\"{0}\",\"remark\":\"{1}\"}}", openid, remark);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取用户基本信息(UnionID机制)
		/// </summary>
		/// <param name="userReq">请求参数</param>
		/// <returns></returns>
		public async Task<WxOffcialUserInfoResp> GetUserInfoAsync(WxOffcialUserInfoReq userReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Get;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + string.Format("/cgi-bin/user/info?openid={0}&lang={1}", userReq.openid, userReq.lang);
			return await base.RestCommonOffcialAsync<WxOffcialUserInfoResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取用户基本信息(UnionID机制)列表
		/// </summary>
		/// <param name="userReq">请求参数</param>
		/// <returns></returns>
		public async Task<WxOffcialUserListResp> GetUserInfoListAsync(IList<WxOffcialUserInfoReq> userReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/user/info/batchget";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				user_list = userReq
			});
			return await base.RestCommonOffcialAsync<WxOffcialUserListResp>(LHttpRequest, null);
		}

		/// <summary>
		///  获取标签下用户Openid列表
		/// </summary>
		/// <param name="tagId"></param>
		/// <param name="next_openid">第一个拉取的OPENID，不填默认从头开始拉取</param>
		/// <returns></returns>
		public async Task<WxOpenIdsResp> GetOpenIdListByTagAsync(int tagId, string next_openid = "")
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/user/tag/get";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				tagid = tagId,
				next_openid = next_openid
			});
			return await base.RestCommonOffcialAsync<WxOpenIdsResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 添加标签   最多添加一百个
		/// </summary>
		/// <param name="name">标签名称</param>
		/// <returns></returns>
		public async Task<WxAddTagResp> AddTagAsync(string name)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/tags/create";
			LHttpRequest.CustomBody = string.Format("{{\"tag\":{{\"name\":\"{0}\"}}}}", name);
			return await base.RestCommonOffcialAsync<WxAddTagResp>(LHttpRequest, null);
		}

		/// <summary>
		///  修改标签
		/// </summary>
		/// <param name="id">标签id</param>
		/// <param name="name">标签名称</param>
		/// <returns></returns>
		public async Task<WxBaseResp> UpdateTagAsync(int id, string name)
		{
			LHttpRequest LHttpRequest = new LHttpRequest
			{
				HttpMothed = HttpMothed.Post,
				AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/tags/update"
			};
			LHttpRequest.CustomBody = string.Format("{{\"tag\":{{\"id\":{0},\"name\":\"{1}\"}}}}", id, name);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		///  删除标签
		/// </summary>
		/// <param name="id">标签id</param>
		/// <returns></returns>
		public async Task<WxBaseResp> DeleteTagAsync(int id)
		{
			LHttpRequest LHttpRequest = new LHttpRequest
			{
				HttpMothed = HttpMothed.Post,
				AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/tags/delete"
			};
			LHttpRequest.CustomBody = string.Format("{{\"tag\":{{\"id\":{0}}}}}", id);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		///   获取公众号已创建的标签
		/// </summary>
		/// <returns></returns>
		public async Task<WxGetTagListResp> GetTagListAsync()
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Get;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/tags/get";
			return await base.RestCommonOffcialAsync<WxGetTagListResp>(LHttpRequest, null);
		}

		/// <summary>
		///   给多个用户同时设置或者取消标签
		/// </summary>
		/// <param name="openIdList">要设置的用户列表，数量不能超过50个</param>
		/// <param name="tagId">标签Id</param>
		/// <param name="flag">标识  0. 增加标签     1.  取消标签</param>
		/// <returns></returns>
		public async Task<WxBaseResp> SetOrCancleUsersTagAsync(List<string> openIdList, int tagId, int flag)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/tags/members/" + ((flag == 0) ? "batchtagging" : "batchuntagging");
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				tagid = tagId,
				openid_list = openIdList
			});
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		///  获取用户身上的标签列表
		/// </summary>
		/// <param name="openid"></param>
		/// <returns></returns>
		public async Task<WxGetUserTagsResp> GetUserTagsByOpenId(string openid)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/tags/getidlist";
			LHttpRequest.CustomBody = string.Format("{{\"openid\":\"{0}\"}}", openid);
			return await base.RestCommonOffcialAsync<WxGetUserTagsResp>(LHttpRequest, null);
		}

		/// <summary>
		///   获取黑名单用户Openid列表
		/// </summary>
		/// <param name="next_openid"></param>
		/// <returns></returns>
		public async Task<WxOpenIdsResp> GetBlackOpenIdListAsync(string next_openid)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/tags/members/getblacklist";
			LHttpRequest.CustomBody = string.Format("{{\"begin_openid\":\"{0}\"}}", next_openid);
			return await base.RestCommonOffcialAsync<WxOpenIdsResp>(LHttpRequest, null);
		}

		/// <summary>
		///   批量拉黑用户
		/// </summary>
		/// <param name="openids">openid列表</param>
		/// <returns></returns>
		public async Task<WxBaseResp> BatchBlackOpenIds(IList<string> openids)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/tags/members/batchblacklist";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				opened_list = openids
			});
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		///   批量取消拉黑用户
		/// </summary>
		/// <param name="openids">openid列表</param>
		/// <returns></returns>
		public async Task<WxBaseResp> BatchUnBlackOpenIds(IList<string> openids)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/tags/members/batchunblacklist";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				opened_list = openids
			});
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}
	}
}
